<?php

/*
 * This will be the Create Position page. 
 * 
 * Created by: Peter Agno Jr.
 * Date created: November 27, 2011
 */

    include("siteTop.php"); 
    include('includes/siteConfig.php'); 

    // connect to database
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
?>

<!-- HEADER START -->
    <!--JAVASCRIPT SOURCES-->
    <script src="js/jquery-1.6.2.min.js"></script>
    
    <!--ACTUAL JS SCRIPTS AND JQUERIES-->
    <script type="text/javascript">
        jQuery(document).ready(function(){ 
            var rowDept = 0, rowSecGrp = 0;
            
            // CHANGE FIRST THE CSS OF THE NAV BAR
            jQuery("#adminTools-nav").addClass("highlightMenuBar");
            jQuery("#orgSetup-nav").addClass("highlightMenuBar");
            jQuery("#posSetup-nav").addClass("highlightMenuBar");
            
            jQuery("#positionId").focus();
            addPosDeptRow(); // Add a position's department row
            addPosSecGrpRow(); // Add a position's security group row
            //
            // Start - Perform click when user pressed enter
            jQuery(".inputbox").keypress(function(e){
                if(e.keyCode == 13) {
                    jQuery("#createPosButton").click();
                }
            }); // End - Perform submit when user pressed enter
            
            // Start - create position
            jQuery('#createPosButton').live("click",function() {
                if ( jQuery("#positionId").val() == "" || jQuery("#positionName").val() == ""  ) {
                    // Show error message if the user clicked the create button
                    // but haven't inputted all of the neccessary information.
                    alert("Please input the required information.");
                }
                else if ( noSameDept() != true ) {
                    alert("Error : Position should not have two same department.");
                }
                else if ( noSameSecGrp() != true ) {
                    alert("Error : Position should not have two same security group.");
                }
                else {
                    // Start - Ajax the creating of position                    
                    jQuery.ajax({
                        type : 'POST',
                        url : 'ajaxRequests/admCreatePos.php',
                        dataType : 'json',
                        async : false,
                        data: {
                            positionId : jQuery("#positionId").val(),
                            positionName : jQuery("#positionName").val()
                        },
                        success : function(data){
                            // If, successful creation of position
                            if (data) {
                                if ( data[0].flag == "success" ) {
                                    jQuery("#posSuccessMessage").val(data[0].msg);
                                    jQuery("#posStpMsgForm").submit();
                                }
                                else {
                                    alert(data[0].msg);
                                }
                            } // End if, successful creation of position
                        },
                        error : function() {
                            alert("Error : Creating the position was not successful.");
                        }
                    }); // End - Ajax the creating of position
                } // End else
            }); // End - create position
            
            // Start - Add another row in Position's Department Screenlet
            jQuery('#addDeptBtn').live("click",function() {
                addPosDeptRow();
            }); // End - Add another row in Position's Department Screenlet
            
            // Start - Remove a department row
            jQuery('.removeDeptBtn').live("click",function() {
                buttonId = this.id;
                tempSubstr = buttonId.split('_');
                jQuery("#rowDeptId_"+tempSubstr[1]).remove();
                
                // Add a department row if the table row is equal to 1 (header's row)
                if ( jQuery('#posDeptTable tr').length == 1 ) {
                    jQuery("#addDeptBtn").click();
                }
            }); // End - Remove a department row
            
            // Start - Add another row in Position's Security Group Screenlet
            jQuery('#addSecGrpBtn').live("click",function() {
                addPosSecGrpRow();
            }); // End - Add another row in Position's Security Group Screenlet
            
            // Start - Remove a security group row
            jQuery('.removeSecGrpBtn').live("click",function() {
                buttonId = this.id;
                tempSubstr = buttonId.split('_');
                jQuery("#rowSecGrpId_"+tempSubstr[1]).remove();
                
                // Add a department row if the table row is equal to 1 (header's row)
                if ( jQuery('#posSecGrpTable tr').length == 1 ) {
                    jQuery("#addSecGrpBtn").click();
                }
            }); // End - Remove a security group row
            
            // Start - Go back to admPositionSetup.php
            jQuery('#admPosStpBtn').live("click",function() {
                window.location = 'admPositionSetup.php';
            }); // End - Go back to admDepartmentSetup.php 
            
            // Start - Function: Add another row in Position's Department Screenlet
            function addPosDeptRow() {
                jQuery.ajax({
                    type : 'POST',
                    url : 'ajaxRequests/getDepartmentList.php',
                    dataType : 'json',
                    async : false,
                    data: {
                    },
                    success : function(data){
                        if (data) {
                            deptRow = "";
                            rowDept++;
                            deptRow += "<tr id='rowDeptId_"+rowDept+"'>";
                                
                            // Prepare the Department dropdown
                            deptRow += "<td><select name='deptId' id='deptId_"+rowDept+"' class='deptId'>";
                                
                            jQuery.each(data, function(index, data) {
                                deptRow += "<option value='"+data.departmentId+"'>"+data.departmentName+"</option>";
                            });
                            
                            deptRow += "</select></td>";    // Close the Department dropdown
                                
                            // Prepare the Remove button
                            deptRow += "<td><input type='Submit' name='removeDeptBtn' id='removeDeptBtn_"+rowDept+"' value='Remove' class='siteRedButton removeDeptBtn' /></td>";
                            deptRow += "</tr>"; // Close the row
                            jQuery(posDeptTable).append(deptRow);    // Append the row
                        } 
                    },
                    error : function() {
                        alert("Error : Adding department row was not successful.");
                    }
                });
            } // End - Function: Add another row in Position's Department Screenlet
            
            // Start - Function: Add another row in Position's Security Group Screenlet
            function addPosSecGrpRow() {
                jQuery.ajax({
                    type : 'POST',
                    url : 'ajaxRequests/getSecGroupList.php',
                    dataType : 'json',
                    async : false,
                    data: {
                    },
                    success : function(data){
                        if (data) {
                            secGrpRow = "";
                            rowSecGrp++;
                            secGrpRow += "<tr id='rowSecGrpId_"+rowSecGrp+"'>";
                                
                            // Prepare the Security Group dropdown
                            secGrpRow += "<td><select name='secGrpId' id='secGrpId_"+rowSecGrp+"' class='secGrpId'>";
                                
                            jQuery.each(data, function(index, data) {
                                secGrpRow += "<option value='"+data.securityGroupId+"'>"+data.securityGroupName+"</option>";
                            });
                            
                            secGrpRow += "</select></td>";    // Close the Security Group dropdown
                                
                            // Prepare the Remove button
                            secGrpRow += "<td><input type='Submit' name='removeSecGrpBtn' id='removeSecGrpBtn_"+rowSecGrp+"' value='Remove' class='siteRedButton removeSecGrpBtn' /></td>";
                            secGrpRow += "</tr>"; // Close the row
                            jQuery(posSecGrpTable).append(secGrpRow);    // Append the row
                        } 
                    },
                    error : function() {
                        alert("Error : Adding security group row was not successful.");
                    }
                });
            } // End - Function: Add another row in Position's Security Group Screenlet
            
            // Start - Function: Check if there is same department in Position's Department Screenlet
            function noSameDept() {
                noSame = true;
                deptIds = jQuery(".deptId").val();
                
                if ( jQuery('#posDeptTable tr').length == 2 ) {
                    return noSame;
                }
                else {
                    for( x = 1 ; x <= jQuery('#posDeptTable tr').length - 1 && noSame == true ; x++ ){
                        for( y = 1 ; y <= jQuery('#posDeptTable tr').length - 1 && noSame == true ; y++ ){
                            if ( x != y ) {
                                if ( jQuery("#deptId_"+x).val() == jQuery("#deptId_"+y).val()  ) {
                                    noSame = false;
                                }
                            }
                        }
                    }
                    
                    return noSame;
                }
            } // End - Function: Check if there is same department in Position's Department Screenlet
            
            // Start - Function: Check if there is same security group in Position's Security Group Screenlet
            function noSameSecGrp() {
                noSame = true;
                deptIds = jQuery(".secGrpId").val();
                
                if ( jQuery('#posSecGrpTable tr').length == 2 ) {
                    return noSame;
                }
                else {
                    for( x = 1 ; x <= jQuery('#posSecGrpTable tr').length - 1 && noSame == true ; x++ ){
                        for( y = 1 ; y <= jQuery('#posSecGrpTable tr').length - 1 && noSame == true ; y++ ){
                            if ( x != y ) {
                                if ( jQuery("#secGrpId_"+x).val() == jQuery("#secGrpId_"+y).val()  ) {
                                    noSame = false;
                                }
                            }
                        }
                    }
                    
                    return noSame;
                }
            } // End - Function: Check if there is same security group in Position's Security Group Screenlet
            
        }); // End - jQuery document ready
    </script>
<!-- HEAD END -->

<div id="content">
<h1>Create Position</h1>
    <div class="contentbox">       
        <?php
        // Check permission
        $thePermissions = array('POSSTP');
        if ( checkPermission( $thePermissions, $userId ) == true ) {
        ?>
            <span class="screenlet" >Position Information</span>
            <table>
                <tr>
                    <td>Position ID : </td>
                    <td>
                        <input type="text" value="" name="positionId" id="positionId" class="inputbox"/>
                    </td>
                </tr>
                <tr>
                    <td>Position Name : </td>
                    <td>
                        <input type="text" value="" name="positionName" id="positionName" class="inputbox"/>
                    </td>
                </tr>
            </table>
            
<!--            <span class="screenlet" >Position's Department</span>
            <table border="1" id="posDeptTable">
                <tr>
                    <th>Department</th>
                    <th><input type="Submit" name="addDeptBtn" id="addDeptBtn" value="Add" class="siteGreenButton" /></th>
                </tr>
            </table>-->
            
<!--            <span class="screenlet" >Position's Security Group</span>
            <table border="1" id="posSecGrpTable">
                <tr>
                    <th>Security Group</th>
                    <th><input type="Submit" name="addSecGrpBtn" id="addSecGrpBtn" value="Add" class="siteGreenButton" /></th>
                </tr>
            </table>-->
            
            <input type="Submit" name="createPosButton" id="createPosButton" value="Create" class="siteGreenButton" />
            <input type="Submit" name="admPosStpBtn" id="admPosStpBtn" value="Back" class="siteGreenButton" />
            
            <!-- Hidden form to be used when the creation of position was successful -->
            <form id="posStpMsgForm" name="posStpMsgForm" action="admPositionSetup.php" method="POST">
                <input type="hidden" name="posSuccessMessage" id="posSuccessMessage" value=""/>
            </form>
        <?php
        }
        else {
            echo "<div class='noPermissionMsgBox noPermissionMsg' style='border: 2px solid red'><div id='noPermissionMsgs'>You have no permission to view this page.</div></div>";
        }
        // End checking permission
        ?>
    </div> <!-- contentbox end div -->
    <div style="position:relative; visibility: hidden">end</div>    <!-- extra space for contentbox -->
</div> <!-- content end div -->
<?php
    include ("siteBottom.php"); 
?>

